namespace SubhadraSolutions.Sharp.Logging.Loggers
{
    using SubhadraSolutions.Sharp.Logging.Context;
    using SubhadraSolutions.Sharp.Logging.Contracts;
    using SubhadraSolutions.Sharp.Logging.LogItems;
    using SubhadraSolutions.Sharp.Logging.LogPublishers;
    using System;

    public class BufferedLogger : AbstractBufferedLogPublisher, ILogger
    {
        #region Constructors

        public BufferedLogger()
        {
            ExcludeNullContextValues = true;
        }

        #endregion Constructors

        #region Properties

        public bool ExcludeNullContextValues
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        public void Log(LogItemSeverity severity, LogItemCategories categories, string message, Exception exception)
        {
            writeToStore(severity, categories, SimpleLogItem.CreateNew(message, exception));
        }

        public void Log(LogItemSeverity severity, LogItemCategories categories, object logEntry)
        {
            writeToStore(severity, categories, logEntry);
        }

        public void Log(ILogItem logEntry)
        {
            writeToStore(logEntry.Severity, logEntry.Categories, logEntry);
        }

        public void Log(LogStoreItem item)
        {
            LogStore.Write(item);
        }

        private void writeToStore(LogItemSeverity severity, LogItemCategories categories, object logEntry)
        {
            var item = new LogStoreItem(severity, categories, logEntry)
            {
                ContextValues = LoggingContext.Instance.GetAll(ExcludeNullContextValues)
            };
            LogStore.Write(item);
        }

        #endregion Methods
    }
}